package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.DomainCertificateService;
import com.yswy.backstage.service.UserProjectService;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.Domain;
import com.yswy.domain.entity.DomainCertificate;
import com.yswy.domain.entity.PackageVersion;
import com.yswy.domain.entity.UserProject;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.DomainCertificateMapper;
import com.yswy.domain.mapper.UserProjectMapper;
import com.yswy.domain.vo.gld.addOrUpate.DomainCertificateAddOrUpdate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
@AllArgsConstructor
public class DomainCertificateServiceImpl extends ServiceImpl<DomainCertificateMapper, DomainCertificate>
        implements DomainCertificateService {
    @Autowired
    private HuaWeiYunOBSUtil huaWeiYunOBSUtil;
    @Override
    public List<DomainCertificate> listCertificateByDomainId(Long domainId) {
        return list(new LambdaQueryWrapper<DomainCertificate>()
                .eq(DomainCertificate::getDomainId,domainId)
                .eq(DomainCertificate::getIsDel , 0));
    }


    @Override
    public void addOrUpadteCertificate(DomainCertificateAddOrUpdate res) {
        User user = CurrentUserContext.getCurrentUser();
        if (res.getCertificateFile() != null ){
            String fileUrl = huaWeiYunOBSUtil.toUrl(res.getCertificateFile() , "datePlatform/DominCertificate");
            res.setCertificateUrl(fileUrl);
        }
        Boolean isExist =  isExist(res.getDomainName());

        if (res.getId() != null){
            //更新数据
            DomainCertificate bo = getById(res.getId());
            Assert.state(null != bo && null != bo.getId(), NULL_MSG);
            BeanUtil.copyProperties(res, bo);
            BasePojo.setBase(user.getId().longValue(), false, bo);
            updateById(bo);
        }else {
            if (isExist){
                throw new IllegalStateException("域名已存在");
            }
            DomainCertificate domainCertificate = new DomainCertificate(res , user);
            save(domainCertificate);
        }
    }

    @Override
    public Boolean isExist(String domainName) {
        Integer count = count(new LambdaQueryWrapper<DomainCertificate>()
                .eq(DomainCertificate::getDomainName , domainName)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }
        return false;
    }
    @Override
    public void delCertificate(Long domainCertificateId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, domainCertificateId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        DomainCertificate bo = new DomainCertificate();
        bo.setId(domainCertificateId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }
}
